﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Net.Mail;

namespace ITRequest.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        protected Dictionary<string, int> _aEquipment = new Dictionary<string, int>();
                   
        protected void _init()
        {
            _aEquipment.Clear();
            _aEquipment.Add("Mouse", 1);
            _aEquipment.Add("Monitor", 5);
            _aEquipment.Add("Keyboard", 1);
            _aEquipment.Add("Desktop", 14 );
            _aEquipment.Add("Desktop (Student)", 14);
            _aEquipment.Add("Laptop (Remote Worker)", 14);
            _aEquipment.Add("Laptop (Office Full Setup)", 14);
            _aEquipment.Add("Laptop (Student)", 14);
            _aEquipment.Add("Printer (Desktop B/W)", 7);
            _aEquipment.Add("Printer (Desktop Color)", 7);
            _aEquipment.Add("Cell Phone Droid", 5);
            _aEquipment.Add("Cell Phone iPhone", 5);
            _aEquipment.Add("Cell Phone Non-Data", 5);
            _aEquipment.Add("iPad", 5);
            _aEquipment.Add("Desktop Phone", 5);
        }
        public ActionResult About()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Submit(FormCollection collection)
        {
            
            string sFrom = collection["requester"];
            string sLocation = collection["location"];
            collection.Remove("requester");
            collection.Remove("submit");
            collection.Remove("location");
            StringBuilder oOutput = new StringBuilder();
            StringBuilder oResult = new StringBuilder();
            foreach (string sKey in collection.AllKeys)
            {
                if (!sKey.ToLower().Equals("submit"))
                {
                    string sValue = collection[sKey];
                    if (!String.IsNullOrEmpty(sValue))
                    {
                        oOutput.Append(String.Format("{0}:  {1}{2}", sKey, collection[sKey], Environment.NewLine));
                        oResult.Append(String.Format("<b>{0}</b>: {1}<br />", sKey, collection[sKey]));
                    }
                }
            }
            MailMessage mailMessage = new MailMessage();
            mailMessage.Body = oOutput.ToString();
            mailMessage.Subject = String.Format("New IT Request - {0}", sLocation);
            mailMessage.From = new MailAddress(sFrom);
            mailMessage.To.Add(new MailAddress("rgray@lfsva.org"));
            mailMessage.To.Add(new MailAddress("it.support@lfsva.org"));
            SmtpClient smtpClient = new System.Net.Mail.SmtpClient("exec01.lfsva.int");
            smtpClient.Send(mailMessage);
            ViewData["result"] = oResult.ToString();
            return View();
        }

        public JsonResult GetEquipment()
        {
            this._init();
            return Json(_aEquipment, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetEffectiveDate(string sEffectiveDate, string sEquipment)
        {
            this._init();
            DateTime oEffectiveDate = Convert.ToDateTime(sEffectiveDate);
            int iDays = 7; // default to 7 days
            if (_aEquipment.ContainsKey(sEquipment))
            {
                iDays = _aEquipment[sEquipment];
            }
            if (DateTime.Now.AddDays(iDays) > oEffectiveDate) {
                sEffectiveDate =  DateTime.Now.AddDays(iDays).ToShortDateString();
            }
            return Json(new { sEffectiveDate = sEffectiveDate }, JsonRequestBehavior.AllowGet);
        }
    }
}
